#include "include.h"

#ifdef USE_QT

#include "gui/guievent.h"
#include "qt_graphicview.h"
#include "qt_utils.h"



#include "compile/debug_memoryleaks"
using namespace gui;
using namespace qtgui;

GraphicViewQT* GraphicViewQT::create(QWidget* const widget)
{
	assert(widget);
    GraphicViewQT * gvqt = new GraphicViewQT();
	gvqt -> setDelegate_qt_widget(widget);
	return gvqt;
}


GraphicViewQT::GraphicViewQT()
: qt_widget(NULL)
{
    painterqt=GraphicPainterQT::create();
    GraphicView::setPainter(painterqt);
}


GraphicViewQT::~GraphicViewQT()
{
    qt_widget = NULL;
    if (painterqt)
    {
        delete painterqt;
        GraphicView::setPainter(painterqt = NULL);
    }
}


/** This virtual method must be overwritten to return
  the width of the widget the graphic is shown in */
int GraphicViewQT::getWidth()
{
    if (qt_widget)
    {
        return qt_widget->width();
    }
    else
        return 0;
}

/** This virtual method must be overwritten to return
  the height of the widget the graphic is shown in */
int GraphicViewQT::getHeight()
{
    if (qt_widget)
    {
        return qt_widget->height();
    }
    else
        return 0;
}

void GraphicViewQT::redraw()
{
    if(qt_widget)
    {
        qt_widget->repaint();
    }
}


void GraphicViewQT::setDelegate_qt_widget(QWidget* qtwidget)
{
    qt_widget = qtwidget;
    painterqt->set_qt_widget(qtwidget);
}

//////////////////////////////////////////////////////////////

/*
void GraphicViewQT::repaint_from_qt(QPaintEvent* event)
{
    if (qt_widget)
    {
        this->beginPainterQT();

        this->paintAll();

        this->endPainterQT();
    }

}


void GraphicViewQT::resize_from_qt(QResizeEvent *event)
{
    assert(event);
    const SizeEvent& sizeevent = qtgui::buildEvent(*event);
    GraphicView::onResize(sizeevent);
}

void GraphicViewQT::mouse_press_from_qt(QMouseEvent *event)
{
    assert(event);
    const MouseEvent& mouseevent = qtgui::buildEvent(*event);
    GraphicView::onMouseEvent(mouseevent);
}
void GraphicViewQT::mouse_release_from_qt(QMouseEvent *event)
{
    assert(event);
    const MouseEvent& mouseevent = qtgui::buildEvent(*event);
    GraphicView::onMouseEvent(mouseevent);
}
void GraphicViewQT::mouse_move_from_qt(QMouseEvent *event)
{
    assert(event);
    const MouseEvent& mouseevent = qtgui::buildEvent(*event);
    GraphicView::onMouseEvent(mouseevent);
}
void GraphicViewQT::wheel_from_qt(QWheelEvent *event)
{
    std::cout << event->type() << std::endl;
}
void GraphicViewQT::key_press_from_qt(QKeyEvent *event)
{
    std::cout << event->type() << std::endl;
}
void GraphicViewQT::key_release_from_qt(QKeyEvent *event)
{
    std::cout << event->type() << std::endl;
}
*/

void GraphicViewQT::beginPainterQT()
{
    if (painterqt)
    {
        painterqt->qt_begin_painter();
    }
}

void GraphicViewQT::endPainterQT()
{
    if (painterqt)
    {
        painterqt->qt_end_painter();
    }
}





#endif
